﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using DiBang.Sql;

namespace ChangWuDiBang
{
    public partial class FrmUserManager : Office2007Form
    {
        public UserInfo _UserInfo
        {
            get;
            set;
        }

        SqlLib sqllib = new SqlLib();
        SqlExtend sqlextend = new SqlExtend();

        public FrmUserManager()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void FrmUserManager_Load(object sender, EventArgs e)
        {
            if (_UserInfo == null)
                return;

            if (!_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
            {
                MessageBoxEx.Show("不是管理员不能管理账户!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SetGrid();
        }

        private void SetGrid()
        {
            string ssql = @"SELECT USER_ID,USER_NAME,USER_PWD,USER_GROUP,ACTIVE,LAST_EVENT_NAME,
LAST_EVENT_USER_ID,LAST_EVENT_TIMEKEY FROM DB_USER";

            DataTable dt = sqllib.GetDataSet(ssql).Tables[0];
            dgvUserInfo.DataSource = dt;
        }

        private void btnRefect_Click(object sender, EventArgs e)
        {
            SetGrid();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNewAdd_Click(object sender, EventArgs e)
        {
            FrmUserManagerModify frm = new FrmUserManagerModify();
            frm._UserInfo = this._UserInfo;
            frm.Mode = "ADD_NEW";
            frm.ShowDialog();

            SetGrid();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgvUserInfo.CurrentRow == null)
            {
                MessageBoxEx.Show("请先选择需要修改的列!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            UserInfo modifyUser = new UserInfo();
            modifyUser.UserID = dgvUserInfo.CurrentRow.Cells["USER_ID"].Value.ToString();
            modifyUser.UserName = dgvUserInfo.CurrentRow.Cells["USER_NAME"].Value.ToString();
            modifyUser.UserPwd = dgvUserInfo.CurrentRow.Cells["USER_PWD"].Value.ToString();
            modifyUser.UserGroup = dgvUserInfo.CurrentRow.Cells["USER_GROUP"].Value.ToString();
            modifyUser.Active = dgvUserInfo.CurrentRow.Cells["ACTIVE"].Value.ToString();

            if (_UserInfo.UserID == modifyUser.UserID)
            {
                MessageBoxEx.Show("不能修改本人信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FrmUserManagerModify frm = new FrmUserManagerModify();
            frm._UserInfo = this._UserInfo;
            frm._ModifyUserInfo = modifyUser;
            frm.Mode = "MODIFY";
            frm.ShowDialog();

            SetGrid();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int retInt = -1;
            if (dgvUserInfo.CurrentRow == null)
            {
                MessageBoxEx.Show("请先选择需要修改的列!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            UserInfo modifyUser = new UserInfo();
            modifyUser.UserID = dgvUserInfo.CurrentRow.Cells["USER_ID"].Value.ToString();
            modifyUser.UserName = dgvUserInfo.CurrentRow.Cells["USER_NAME"].Value.ToString();
            modifyUser.UserPwd = dgvUserInfo.CurrentRow.Cells["USER_PWD"].Value.ToString();
            modifyUser.UserGroup = dgvUserInfo.CurrentRow.Cells["USER_GROUP"].Value.ToString();
            modifyUser.Active = dgvUserInfo.CurrentRow.Cells["ACTIVE"].Value.ToString();

            if (_UserInfo.UserID == modifyUser.UserID)
            {
                MessageBoxEx.Show("不能删除本人信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FrmUserManagerModify frm = new FrmUserManagerModify();
            frm._UserInfo = this._UserInfo;
            frm._ModifyUserInfo = modifyUser;
            frm.Mode = "DELETE";
            frm.ShowDialog();

            SetGrid();

            //DialogResult diaResult = MessageBoxEx.Show("是否删除"+modifyUser.UserID+"?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (diaResult.Equals(DialogResult.Yes))
            //{
            //    Dictionary<string, object> whereMap = new Dictionary<string, object>();
            //    whereMap.Add("USER_ID", modifyUser.UserID);

            //    retInt=sqllib.DeleteTable("DB_USER", whereMap);

            //    if (retInt >= 0)
            //    {
            //        MessageBoxEx.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }

            //    SetGrid();
            //}
        }
    }
}
